#include "common/settings.h"
#include "common/log/QsLog.h"

#ifdef Q_OS_LINUX
    #include <pwd.h>
#endif
#include <iostream>
#include <unistd.h>
#include <QMap>

QString d_workspace = 0;
QString p_directory = 0;
QString d_airfoils = 0;
QString d_simulator = 0;
QString d_log = 0;
QMap<QString, int> modules;

void set_workspace_path(const QString &path)
{
    if(not d_workspace.isNull())
    {
        d_workspace.clear();
        d_workspace = path;
    }
    else
        d_workspace = path;
}

void set_simulator_path(const QString &path)
{
    if(not d_simulator.isNull())
    {
        d_simulator.clear();
        d_simulator = path;
    }
    else
        d_simulator = path;
}

const QString& workspace_directory()
{
    if(d_workspace == 0)
    {
        #ifdef Q_OS_LINUX
            register struct passwd *pw;
            register uid_t uid;

            uid = geteuid ();
            pw = getpwuid (uid);
            char *user = pw->pw_name;
            QString current_user(user);
            d_workspace =  QString("/home/").append(current_user).append("/gepar_ws/");
        #elif _WIN32
                d_workspace = "./";
        #endif
    }

    return d_workspace;
}


const QString& projects_directory()
{

    if(p_directory.isNull())
    {
        p_directory =  QString(workspace_directory()).append("projects/");
    }

    return p_directory;
}

const QString& airfoils_directory()
{

    if(d_airfoils.isNull())
    {
        d_airfoils =  QString(workspace_directory()).append("airfoils/");
    }
    return d_airfoils;
}

const QString& log_directory()
{
    if(d_log.isNull())
    {
        d_log =  QString(workspace_directory()).append("log/");
    }
    return d_log;
}

/**
  *
  */
int add_module(const QString &module)
{
    int module_value;

    if(not modules.contains(module))
    {
        module_value = modules.size();
        modules.insert(module, module_value);
    }
    else
        module_value = modules[module];

    return module_value;
}

/**
  *
  */
int get_module_value(const QString &module)
{
    int module_value = (modules.contains(module))? modules[module] : -1;

    return module_value;
}

/**
  *
  */
const QString &get_module_key(int module)
{
   return modules.key(module);
}
